package gov.kqqzyyy.monkey.pharmacyscreen.code.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import gov.kqqzyyy.monkey.pharmacyscreen.R;
import gov.kqqzyyy.monkey.pharmacyscreen.code.mytools.CurrentVersion;
import gov.kqqzyyy.monkey.pharmacyscreen.code.mytools.MarqueeTextView;
import gov.kqqzyyy.monkey.pharmacyscreen.code.mytools.PharmacyWindowBaseActivity;
import gov.kqqzyyy.monkey.pharmacyscreen.code.object.Patient;

//中药窗口-代煎窗口、自煎药未配药窗口
@SuppressLint("NonConstantResourceId")
public class TCMDHScreenActivity extends PharmacyWindowBaseActivity {

    @BindView(R.id.tcm_zjcyklj_patient_layout)
    LinearLayout mZjcyKljLayout;
    @BindView(R.id.tcm_dh_patient_zjcy_gridview)
    GridView mZjcyGridView;
    @BindView(R.id.tcm_dh_patient_klj_gridview)
    GridView mKljGridView;

    @BindView(R.id.tcm_patient_layout)
    LinearLayout mPatientLayout;
    @BindView(R.id.tcm_dh_patient_gridview)
    GridView gridView;

    @BindView(R.id.tcm_dh_pharmacy_window_text)
    TextView mPhaWinText;
    @BindView(R.id.tcm_title_text)
    TextView mTitleText;
    @BindView(R.id.tcm_dh_datetime_text)
    TextView mDateTimeText;
    @BindView(R.id.tcm_dh_version_text)
    TextView mVersionText;
    @BindView(R.id.tcm_warn_text)
    MarqueeTextView mWarnText;

    private String mWindowID;
    private String mPharmacyID;
    private boolean isTomorrow = false;

    private GridView mainGridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tcmscreen);
        ButterKnife.bind(this);
        onBindUI();
    }

    @Override
    protected void onBindUI() {
        super.onBindUI();
        setTimeTextView(mDateTimeText);
//        setMainGridView(gridView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        String mPharmacyName = getmSharedPreferences().getString("pharmacy_name", "");
        String mWindowName = getmSharedPreferences().getString("window_name", "");
        mPharmacyID = getmSharedPreferences().getString("pharmacy_preference", "");
        mWindowID = getmSharedPreferences().getString("window_preference", "");
        if (!Objects.equals(mWindowID, "1.0")) { //自煎窗口再细分
            mWindowID = getmSharedPreferences().getString("window_detail_preference", "");
        }
        if (!Objects.equals(mPharmacyName, "") && !Objects.equals(mWindowName, "")) {
            mPhaWinText.setText(mWindowName);
        } else {
            Toast.makeText(getApplicationContext(), "请设置药房窗口！", Toast.LENGTH_SHORT).show();
        }
        switch (Objects.requireNonNull(mWindowID)) {
            case "1.0": //代煎窗口
                String text = getResources().getString(R.string.tcm_window_text_1);
                mTitleText.setText(text);
                mPatientLayout.setVisibility(View.VISIBLE);
                mZjcyKljLayout.setVisibility(View.GONE);
                mainGridView = gridView;
                setChangeTypeFace(false);
                break;
            case "2.1":
                mPatientLayout.setVisibility(View.GONE);
                mZjcyKljLayout.setVisibility(View.VISIBLE);
                mainGridView = mZjcyGridView;
                mTitleText.setText(getString(R.string.tcm_window_text_2));
                mWarnText.setGravity(Gravity.CENTER);
                mWarnText.setText(getString(R.string.tcm_window_text_8));
                mPhaWinText.setText("自煎草药\n颗粒剂窗口");
                mPhaWinText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
                break;
        }
        setMainGridView(mainGridView);
        if (mKljGridView != null) {
            setKljGridView(mKljGridView);
        }
        setColumnNum(4);
    }

    @Override
    public void doSetVersion() {
        super.doSetVersion();
        String text = "版本号：" + CurrentVersion.getVerName(this);
        mVersionText.setText(text);
    }

    @Override
    public void doSetQYSJ() {//取药时间
        super.doSetQYSJ();
        if (mWindowID.equals("1.0")) {
            String text = getResources().getString(R.string.tcm_window_text_1);
            String warnText = getResources().getString(R.string.tcm_window_text_7);

            Date localDateTime = new Date(System.currentTimeMillis());
            localDateTime.setTime(localDateTime.getTime() + getMyApplication().getDeteDelta());
            String today = getMyApplication().getSdfDate().format(localDateTime);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(localDateTime);
            int month = calendar.get(Calendar.MONTH) + 1;
            try {
                Date qysjDate = getMyApplication().getSdfDateTime().parse(today + " " + getString(R.string.zyf_qyfgsj));
                String qysj, fksj;
                if (month >= 5 && month < 10) {
                    fksj = "10:50";
                } else {
                    fksj = "11:00";
                }

                if (localDateTime.getTime() > Objects.requireNonNull(qysjDate).getTime()) {
                    qysj = "明日10:00";
                    isTomorrow = true;
                } else {
                    qysj = "下午16:00";
                    isTomorrow = false;
                }
                text = String.format(text, qysj);
                warnText = String.format(warnText, fksj);
                mWarnText.setText(warnText);
                mTitleText.setText(text);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void doFlushData() {
        super.doFlushData();
        doGetPatients(Integer.parseInt(mPharmacyID), mWindowID, false);
    }

    @Override
    protected void doFilterData() {
        super.doFilterData();
        List<Patient> resultList = getmResultList();
        List<Patient> showList = new ArrayList<>();
        List<Patient> kljList = new ArrayList<>();
        if (resultList != null && resultList.size() > 0) {
            for (Patient patient : resultList
            ) {
                if (mWindowID.equals("1.0")) {
                    String sfrq = patient.getSFRQ().substring(11);
                    if (isTomorrow) {
                        if (patient.getDJYBZ() == 1 && sfrq.compareTo(getString(R.string.zyf_qyfgsj)) > 0) {
                            showList.add(patient);
                        }
                    } else {
                        if (patient.getDJYBZ() == 1 && sfrq.compareTo(getString(R.string.zyf_qyfgsj)) < 0) {
                            showList.add(patient);
                        }
                    }
                } else {
                    if (patient.getPYBZ().equals("0.0") || patient.getPYBZ().equals("-1.0") && (patient.getFYBZ() == null || patient.getFYBZ().equals("0.0"))) {
                        if (patient.getMJBZ() == 1) {
                            kljList.add(patient);
                        } else {
                            showList.add(patient);
                        }

                    }
                }
            }
            setmShowList(showList);
            setKljList(kljList);
        }
        new CountDownTimer(10000, 5000) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                doFlushData();
            }
        }.start();
    }

    @Override
    protected void doRollingGridView() {
        super.doRollingGridView();
        if (mainGridView != null) {
            rollingGridView(mainGridView, 1);
        }
        if (mKljGridView != null) {
            rollingGridView(mKljGridView, 1);
        }
    }

}